import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'  // 添加Message导入
const Router = VueRouter  // 添加Router别名
Vue.use(VueRouter)

const Login = () => import('../views/login') //登录

// 阅卷管理
const Markingexamlist =() => import('../views/markManage') //阅卷列表
const Progressstatistics = () => import('../views/markManage/progressstatistics')//数据统计
const Reportprogress = () => import('../views/markManage/children/reportprogress') //上报进度
const Answerprogress = () => import('../views/markManage/children/answerprogress') //答卷进度
const Gradingprogress = () => import('../views/markManage/children/gradingprogress') //阅卷进度
const Processmanagement = () => import('../views/markManage/children/processmanagement') //流程管理
const Taskmanage = () => import('../views/markManage/children/taskmanage') //分配任务
// 成绩管理
const DataAnalysis = () => import('../views/scoreAnalysis/dataAnalysis') // 数据分析工具
const Confirmmain = () => import('../views/scoreAnalysis/confirmmain') //信息确认
const Examinationinfo = () => import('../views/scoreAnalysis/children/examinationinfo') // 考试信息
const Schoolinfo = () => import('../views/scoreAnalysis/children/schoolinfo') // 学校信息
const Studentinfo = () => import('../views/scoreAnalysis/children/studentinfo') // 学生信息
const Totalscore = () => import('../views/scoreAnalysis/children/totalscore') //总分成绩信息
const Subtopicinformation = () => import('../views/scoreAnalysis/children/subtopicinformation') //小题分信息
const Onlineinfo = () => import('../views/scoreAnalysis/children/onlineinfo') //上线信息
const Generateinformation = () =>import('../views/scoreAnalysis/children/generateinformation') //确认生成信息
const Generatescoreanalysis = () => import('../views/scoreAnalysis/children/generatescoreanalysis') //生成成绩分析
const Generatepdf = () => import('../views/scoreAnalysis/children/generatepdf') //生成pdf
const Generateatrace = () => import('../views/scoreAnalysis/children/generateatrace') //生成有痕
// 项目管理
const Projectmanage = () => import('../views/projectManage') //项目管理列表
const Maincover = () => import('../views/projectManage/maincover') //项目封面
const Projectoverview = () => import('../views/projectManage/children/projectoverview') //项目概况
const Referenceschools = () => import('../views/projectManage/children/referenceschools') //参考学校
const Studentinfoexam = () => import('../views/projectManage/children/studentinfo') //学生信息
const Teacherinfo = () => import('../views/projectManage/children/teacherinfo') //教师信息

// 考试设置
const Examsetting = () => import('../views/examSetting') //考试设置
const Mainexam = () => import('../views/examSetting/mainexam') //模板管理主页
const Textstructure = () => import('../views/examSetting/children/textstructure') //文本结构
const Modelindex = () => import('../views/examSetting/children/modelindex') //模型
const Templateinformation = () => import('../views/examSetting/templateinformation') //模板信息
const Locationarea = () => import('../views/examSetting/locationarea') //地区设置
const Admissionticketnumberarea = () => import('../views/examSetting/admissionticketnumberarea') //准考证号设置
const Objectivequestion = () => import('../views/examSetting/objectivequestionarea') //客观题
const Subjectivequestion = () => import('../views/examSetting/subjectivequestions') //主观题

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  routes: [
    {
      path: '/login',
      name: 'Login',
      component:Login,
      meta:{title:'登录'}
    },
    {
      path:'/markingexamlist',
      name:'Markingexamlist',
      component:Markingexamlist,
      meta:{title:'阅卷管理'}
    },
    {
      path:'/progressstatistics',
      name: 'Progressstatistics',
      component:Progressstatistics,
      meta:{title:'阅卷管理'},
      children:[
        {
          path:'reportprogress',
          name: 'Reportprogress',
          component:Reportprogress,
          meta:{title:'阅卷管理-上报进度'}
        },
        {
            path: 'answerprogress',
            name: 'Answerprogress',
            component:Answerprogress,
            meta:{title:'阅卷管理-上传答卷进度'}
        },
        {
            path: 'gradingprogress',
            name: 'Gradingprogress',
            component:Gradingprogress,
            meta:{title:'阅卷管理-阅卷进度'}
        },
        {
          path:'processmanagement',
          name: 'Processmanagement',
          component:Processmanagement,
          meta:{title:'阅卷管理-流程管理'}
        },
        {
          path:'taskmanage',
          name: 'Taskmanage',
          component:Taskmanage,
          meta:{title:'阅卷管理-分配任务'}
        },
      ]
    },
    {
      path: '/dataAnalysis',
      name: 'DataAnalysis',
      component:DataAnalysis,
      meta:{title:'数据分析工具'}
    },
    {
      path:'/confirmmain',
      name: 'Confirmmain',
      component:Confirmmain,
      meta:{title:'信息确认'},
      children:[
        {
          path: 'examinationinfo', 
          meta:{title:'考试信息'},
          component: Examinationinfo,
          name:'Examinationinfo',
        },
        {
          path: 'schoolinfo', 
          meta:{title:'学校信息'},
          component: Schoolinfo,
          name:'Schoolinfo',
        },
        {
          path: 'studentinfo', 
          meta:{title:'学生信息'},
          component: Studentinfo,
          name:'Studentinfo',
        },
        {
          path: 'totalscore', 
          meta:{title:'总分成绩信息'},
          component: Totalscore,
          name:'Totalscore',
        },
        {
          path: 'subtopicinformation',
          meta:{title:'小题分信息'},
          component: Subtopicinformation,
          name:'Subtopicinformation',
        },
        {
          path:'onlineinfo',
          meta:{title:'上线信息'},
          component:Onlineinfo,
          name:'Onlineinfo',
        },
        {
          path:'generateinformation',
          meta:{title:'信息生成'},
          component:Generateinformation,
          name:'Generateinformation',
        },
        {
          path:'generatescoreanalysis',
          meta:{title:'生成成绩分析'},
          component:Generatescoreanalysis,
          name:'Generatescoreanalysis',
        },
        {
          path:'generatepdf',
          meta:{title:'生成PDF'},
          component:Generatepdf,
          name:'Generatepdf',
        },
        {
          path:'generateatrace',
          meta:{title:'生成有痕'},
          component:Generateatrace,
          name:'Generateatrace',
        }
      ],
    },
    {
      path:'/projectmanage',
      name: 'Projectmanage',
      component:Projectmanage,
      meta:{title:'项目管理'},
    },
    {
      path:'/maincover',
      name: 'Maincover',
      component:Maincover,
      meta:{title:'项目管理-项目概况'},
      redirect: '/maincover/projectoverview',  // 添加默认重定向
      children:[
        {
          path:'projectoverview',
          name: 'Projectoverview',
          component:Projectoverview,
          meta:{title:'项目管理-项目概况'}
        },
        {
          path:'referenceschools',
          name: 'Referenceschools',
          component:Referenceschools,
          meta:{title:'项目管理-参试学校'}
        },
        {
          path:'studentinfo',
          name: 'Studentinfoexam',
          component:Studentinfoexam ,
          meta:{title:'项目管理-考生信息'}
        },
        {
          path:'teacherinfo',
          name: 'Teacherinfo',
          component:Teacherinfo,
          meta:{title:'项目管理-评阅教师'}
        },
      ]
    },
    {
      path:'/examsetting',
      name: 'Examsetting',
      component:Examsetting,
      meta:{title:'考试设置'},
    },
    {
      path:'/mainexam',
      name: 'Mainexam',
      component:Mainexam,
      meta:{title:'考试设置'},
      redirect: '/mainexam/textstructure',  // 添加默认重定向
      children:[
        {
          path:'textstructure',
          name: 'Textstructure',
          component:Textstructure,
          meta:{title:'考试设置-试题结构'}
        }, 
        {
          path:'modelindex',
          name: 'Modelindex',
          component:Modelindex,
          meta:{title:'考试设置-考试模板'}
        }
      ]
    }, 
    {
      path:'/templateinformation',
      name: 'Templateinformation',
      component:Templateinformation,
      meta:{title:'考试设置-模板信息'}
    }, 
    {
      path:'/locationarea',
      name: 'Locationarea',
      component:Locationarea,
      meta:{title:'考试设置-地区设置'}
    }, 
    {
      path:'/admissionticketnumberarea',
      name: 'Admissionticketnumberarea',
      component:Admissionticketnumberarea,
      meta:{title:'考试设置-准考证号设置'}
    }, 
    {
      path:'/objectivequestion',
      name: 'Objectivequestion',
      component:Objectivequestion,
      meta:{title:'考试设置-客观题'}
    }, 
    {
      path:'/sbjectivequestion',
      name: 'Subjectivequestion',
      component:Subjectivequestion,
      meta:{title:'考试设置-主观题'}
    },  
    {
      path:'/',
      redirect:'/login'
    },
  ]
})

export default router

//路由全局守卫功能
router.beforeEach((to,from,next)=>{
    //判断是否登录进入
  //   console.log(to,'oooooooooooooooooooooo');
      const token = localStorage.getItem("accessToken")
      if (to.matched.some(record => record.meta.isLogin)){  // 判断该路由是否需要登录权限
          if (token) {  // 判断当前的token是否存在
            next();
          }
          else {
            next({
              path: '/login'
            })
          }
        }else {
          next();
        }
      if (to.path.indexOf('wrongTitleInTheBackground') > -1) {
          // 进入错题本后台
          let tokenObj = JSON.parse(localStorage.getItem('tokenInfo'))
          if (!tokenObj) {
              Message.error('登陆已失效！')
              next({
                  path: '/login'
              })
          }
      }
      // 根据路由元信息设置文档标题
      window.document.title = to.meta.title
      // 让页面回到顶部
      document.body.scrollTop = document.documentElement.scrollTop = 0;
      next()
  })
  
  const VueRouterPush = Router.prototype.push
  Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
  }
  
  /** 解决跳转重复路由报错问题 */
  const routerPush = router.push;
  router.push = path => {
      // 判断下当前路由是否就是要跳转的路由
      if (router.currentRoute.fullPath.includes(path)) return;
      return routerPush.call(router, path);
  }